package org.example.device.control;

import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 通用设备控制器
 * 提供通用设备的控制逻辑，作为默认控制器
 */
@Component
public class GenericDeviceController extends AbstractDeviceController {
    
    private static final String DEVICE_TYPE = "GENERIC_DEVICE";
    private static final List<String> SUPPORTED_COMMANDS = Arrays.asList(
        "switch", "status", "properties", "ping"
    );
    
    @Override
    public Map<String, Object> getDeviceProperties(Long deviceId) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("deviceType", DEVICE_TYPE);
        properties.put("supportedCommands", SUPPORTED_COMMANDS);
        properties.put("description", "通用智能设备");
        properties.put("version", "1.0.0");
        properties.put("capabilities", Arrays.asList("basic_control", "status_monitoring"));
        return properties;
    }
    
    @Override
    public DeviceControlResult setDeviceProperties(Long deviceId, Map<String, Object> properties) {
        try {
            // 通用设备属性设置
            DeviceControlCommand command = new DeviceControlCommand(deviceId, "properties", properties);
            return executeCommand(command);
        } catch (Exception e) {
            return DeviceControlResult.failure("设置设备属性失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<String> getSupportedCommands(Long deviceId) {
        return new ArrayList<>(SUPPORTED_COMMANDS);
    }
    
    @Override
    public String getDeviceType() {
        return DEVICE_TYPE;
    }
    
    /**
     * 通用设备开关控制
     */
    public DeviceControlResult controlSwitch(Long deviceId, boolean on, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "switch", on);
        command.setUserId(userId);
        command.setAction("设备开关控制");
        return executeCommand(command);
    }
    
    /**
     * 设备状态查询
     */
    public DeviceControlResult getStatus(Long deviceId, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "status", null);
        command.setUserId(userId);
        command.setAction("设备状态查询");
        return executeCommand(command);
    }
    
    /**
     * 设备连接测试
     */
    public DeviceControlResult ping(Long deviceId, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "ping", null);
        command.setUserId(userId);
        command.setAction("设备连接测试");
        return executeCommand(command);
    }
}
